#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
#include "ConfigFileStruct.h"

void write_header(int file_fd, configFileType* cfg_file){
	char temp_string[128];
	temp_string[0]=0;
	
	temp_string[2]=0;
	temp_string[1]=',';
	temp_string[0]=gear_tag;
	sprintf(temp_string+strlen(temp_string),"%d",cfg_file->gearShiftRPM);
	temp_string[strlen(temp_string)+2]=0;
	temp_string[strlen(temp_string)+1]=',';
	temp_string[strlen(temp_string)]=vel_tag;
	sprintf(temp_string+strlen(temp_string),"%d",cfg_file->speedSampling);
	temp_string[strlen(temp_string)+2]=0;
	temp_string[strlen(temp_string)+1]=',';
	temp_string[strlen(temp_string)]=rpm_tag;
	sprintf(temp_string+strlen(temp_string),"%d",cfg_file->rpmSampling);
	temp_string[strlen(temp_string)+2]=0;
	temp_string[strlen(temp_string)+1]=',';
	temp_string[strlen(temp_string)]=acc_tag;
	sprintf(temp_string+strlen(temp_string),"%d",cfg_file->accelometerSampling);
	//add gps
	write(file_fd, temp_string,strlen(temp_string));
	
}

void read_write_serial_data(int file_fd, int console_int){
	char type;
	int time_stamp;
	int data_bytes;
	int data1,data2,data3;
	int offset, expected_bytes;//for reading buffer
	char temp_string2[GPS_bytes+time_bytes+1];
	
	int nbytes;
	char buf[256];
	char temp_string[256];
	int i;
	read(console_int,buf,1);
					
	offset = 1;//keep the tag in buffer on the next read
					
	switch(buf[0])
	{
		case vel_tag:
			expected_bytes=vel_bytes+time_bytes;
			type = vel_tag;
		case GPS_tag:
			expected_bytes=GPS_bytes+time_bytes;
			type = GPS_tag;
		case acc_tag:
			expected_bytes=acc_bytes+time_bytes;
			type = acc_tag;
		case brake_tag:
			expected_bytes=brake_bytes+time_bytes;
			type = brake_tag;
		case gear_tag:
			expected_bytes=gear_bytes+time_bytes;
			type = gear_tag;
		case time_tag:
			expected_bytes=time_bytes;
			type = time_tag;
		case command_tag:
			type = command_tag;
			//do stuff to initiate command mode
	};
	for(i=0;(expected_bytes != 0)&&(i<10000);i++){//i is a garbage timeout 10000 iterations of read hopefully is long enough for even GPS data
		nbytes = read(console_int,buf[offset],expected_bytes);
		expected_bytes -=nbytes;
		offset += nbytes;
	}
	//clear buf to next data point if i==10000
	
	if(expected_bytes ==0){
		//go do stuff with buf
		temp_string[0]=type;//put tag into data
		time_stamp = ((int)buf[1])<<8+(int)buf[2];
		temp_string[1]=',';
		sprintf(temp_string+2,"%d",time_stamp);
		temp_string[strlen(temp_string)+1]=0;//ensure null termination so strlen continues to work
		temp_string[strlen(temp_string)]=',';
		switch(buf[0])
		{
			case vel_tag:
				data_bytes = vel_bytes;
				while(data_bytes>0){
					data1=data1<<8+(int)buf[offset+1-data_bytes];
					data_bytes-=1;
				}
				sprintf(temp_string+strlen(temp_string),"%d",data1);
				
			case GPS_tag:
				strcat(temp_string,buf+3);
			case acc_tag:
				data_bytes=acc_bytes/3;
				while(data_bytes>0){
					data1=data1<<8+(int)buf[offset+1-data_bytes];
					data2=data2<<8+(int)buf[offset+1-data_bytes+acc_x_bytes];
					data3=data3<<8+(int)buf[offset+1-data_bytes+acc_x_bytes+acc_y_bytes];
					data_bytes-=1;
				}
				sprintf(temp_string+strlen(temp_string),"%d",data1);
				sprintf(temp_string+strlen(temp_string),"%d",data2);
				sprintf(temp_string+strlen(temp_string),"%d",data3);
			case brake_tag:
				data_bytes=brake_bytes;
				while(data_bytes>0){
					data1=data1<<8+(int)buf[offset+1-data_bytes];
					data_bytes-=1;
				}
				sprintf(temp_string+strlen(temp_string),"%d",data1);
			case gear_tag:
				data_bytes=gear_bytes;
				while(data_bytes>0){
					data1=data1<<8+(int)buf[offset+1-data_bytes];
					data_bytes-=1;
				}
				sprintf(temp_string+strlen(temp_string),"%d",data1);
			case time_tag:
				data_bytes=time_bytes;
				
			case command_tag:
				data_bytes=command_bytes;
				while(data_bytes>0){
					data1=data1<<8+(int)buf[offset+1-data_bytes];
					data_bytes-=1;
				}
		};
		write(file_fd, temp_string,strlen(temp_string));
		//fwrite(log_file,1,offset,temp_string);	
		offset = 0; //start writing at the beginning of buf again
		return;
	}
	
	////fscanf(console_int,"%s",buf);
	// if(state == start_of_run){
	// expected_bytes =1;
		// nbytes = read(console_int,buf,expected_bytes);
		// switch(buf[0])
		// {
			// case 0: expected_bytes = 19;
					// offset =1;
					// state= receiving;
					// break;
			// case 1: expected_bytes = 19;
					// state= receiving; break;
		// }
		// break;
	// } else if(state == receiving){
		// nbytes = read(console_int,buf[offset],expected_bytes);
		// expected_bytes -=nbytes;
		// offset += nbytes;
		// if(expected_bytes ==0){
			//go do stuff with buf
			
			// state= start_of_run;
		// }
		
	// }
	write(file_fd, buf,nbytes);
	printf("read %d bytes: text:%s\n",nbytes,buf);

}
// data* get_GPS(char* buf, int end_data){
	// int offset;
	
	// offset = 0;
	
// }


// data_string* data2string(data* old){
// data_string* new;

// }


